﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessObjects;
using DataObjects.EntityFramework.ModelMapper;
using System.Linq.Dynamic;

namespace DataObjects.EntityFramework.Implementation
{
    public class EntityEmployeeDao : IEmployeeDao
    {
        public Employee GetEmployee(int EmployeeId, int SupplierID)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                return Mapper.Map(context.EmployeeEntities.FirstOrDefault(e => ((e.EmployeeId == EmployeeId) && (e.SupplierId == SupplierID))));
            }
        }
        public List<Employee> GetEmployee(string sortExpression = "EmployeeId ASC")
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var list = new List<Employee>();

                var employees = context.EmployeeEntities.AsQueryable().OrderBy(sortExpression).ToList();
                foreach (var employee in employees)
                    list.Add(Mapper.Map(employee));

                return list;
            }
        }
        public void InsertEmployee(Employee employee)
        {
            var entity = Mapper.Map(employee);

            using (var context = DataObjectFactory.CreateContext())
            {
                context.EmployeeEntities.AddObject(entity);
                context.SaveChanges();

                // update business object with new version and id
                employee.EmployeeId = entity.EmployeeId;

                //country.Version = Convert.ToBase64String(entity.Version);
            }
        }
        public void UpdateEmployee(Employee employee)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var entity = context.EmployeeEntities.Where(e => e.EmployeeId == employee.EmployeeId).SingleOrDefault();

                entity.EmployeeId = employee.EmployeeId;
                entity.SupplierId = employee.SupplierId;
                entity.Name = employee.Name;
                entity.Picture = employee.Picture;
                entity.Sex = employee.Sex;
                entity.YahooId = employee.YahooId;
                entity.SkypeId = employee.SkypeId;
                entity.GoogleId = employee.GoogleId;
                entity.FacebookId = employee.FacebookId;
                entity.Telephone = employee.Telephone;
                entity.MobilePhone = employee.MobilePhone;
                //entity.Version = employee.Version;
                entity.Status = employee.Status;

                context.EmployeeEntities.ApplyCurrentValues(entity);
                context.SaveChanges();

                // Update business object with new version
                employee.Version = Convert.ToBase64String(entity.Version);
            }
        }
        public void DeleteEmployee(Employee employee)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var entity = context.EmployeeEntities.Where(c => c.EmployeeId == employee.EmployeeId).SingleOrDefault();
                context.EmployeeEntities.DeleteObject(entity);
                context.SaveChanges();
            }
        }
    }
}
